package com.easyj.base.controller;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.propertyeditors.StringTrimmerEditor;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;

import com.easyj.base.security.SimpleUser;

/**
 * 
 * @author hjf
 * 为controller
 */
public abstract class BaseController {
	 public Logger log =  LoggerFactory.getLogger(getClass());
	 
	 public int MaxLimit=1000;
	
	/**
	 * 统一数据转换
	 * 表单提交时，将将空字符串转为nul
	 * 使用json交换数据，此项已没有了
	 */
	 @InitBinder
	 public void initBinder(WebDataBinder binder) {
		    //StringTrimmerEditor(false)只进行trim; StringTrimmerEditor(true) trim and 将空字符串转为null
	        binder.registerCustomEditor(String.class, new StringTrimmerEditor(true));
	        //日期格式，在实体具体定义，使用@DateTimeFormat(pattern="yyyy-MM-dd")
	        
	        /** 统一日期格式转换
	        //org.springframework.beans.propertyeditors.CustomDateEditor
	        //SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-mm-dd");
	       // binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));
            */
	 }
	 
	 public Long getUserId(){
			SimpleUser p=(SimpleUser)SecurityContextHolder.getContext().getAuthentication().getPrincipal();
			return p.getId();
	}	
		
	public String getUserName(){
			SimpleUser p=(SimpleUser)SecurityContextHolder.getContext().getAuthentication().getPrincipal();
			return p.getName();
	}
		
	public String getUserLoginName(){
			SimpleUser p=(SimpleUser)SecurityContextHolder.getContext().getAuthentication().getPrincipal();
			return p.getUsername();
	}
}
